import Torrent from "../schema/torrent";

/**
 * 更新所有种子评分
 */
export const updateAllTorrentScores = async () => {
  try {
    console.log("开始更新所有种子评分...");
    const torrents = await Torrent.find({});
    console.log(`找到 ${torrents.length} 个种子`);

    let updateCount = 0;

    for (const torrent of torrents) {
      try {
        await torrent.updateScoreStats();
        updateCount++;

        // 每50个打印一次进度
        if (updateCount % 50 === 0) {
          console.log(`已更新 ${updateCount}/${torrents.length} 个种子评分`);
        }
      } catch (error) {
        console.error(`更新种子 ${torrent.infoHash} 评分失败:`, error);
      }
    }

    console.log(
      `完成评分更新: ${updateCount}/${torrents.length} 个种子成功更新`
    );

    return {
      updated: updateCount,
      total: torrents.length,
    };
  } catch (error) {
    console.error("批量更新评分失败:", error);
    return { updated: 0, total: 0 };
  }
};
